package com.lovo.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.bean.Throne;
import com.lovo.bean.ThroneDistribution;
import com.lovo.dao.ThroneMapper;
import com.lovo.service.ThroneService;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class ThroneServiceImpl implements ThroneService {
    @Override
    public PageInfo<Throne> getThrones(int pageNo, int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        PageHelper.startPage(pageNo,parseInt);
        List<Throne> list = mapper.selectAll();
        PageInfo<Throne> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public PageInfo<Throne> getThronesByQuery(int parseInt, int parseInt1, String content) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        PageHelper.startPage(parseInt,parseInt);
        List<Throne> list = mapper.selectByQuery(content);
        PageInfo<Throne> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public boolean addThrone(Throne throne) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        int i = mapper.insertSelective(throne);
        SqlSessionUtil.commit();
        return i>0;
    }

    @Override
    public boolean deleteThrone(int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        int i = mapper.deleteByPrimaryKey(parseInt);
        SqlSessionUtil.commit();
        return i >0 ;
    }

    @Override
    public boolean updateThrone(Throne t) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        int i = mapper.updateByPrimaryKeySelective(t);
        SqlSessionUtil.commit();
        return i >0 ;
    }

    @Override
    public PageInfo<ThroneDistribution> getAllThrones() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        List<Throne> list = mapper.selectAllThrone();
        List<ThroneDistribution> list1 = new ArrayList<>();
                for (int i = 0;i<list.size();i++){
                    ThroneDistribution cr = new ThroneDistribution();
                    cr.setKey(list.get(i).getThroneId());
                    cr.setLabel(list.get(i).getThroneName());
                    cr.setThroneName(list.get(i).getThroneName());
                    cr.setThroneId(list.get(i).getThroneId());
                    cr.setThroneDesc(list.get(i).getThroneDesc());
                    cr.setThronePath(list.get(i).getThronePath());
                    list1.add(cr);
                }
        PageInfo<ThroneDistribution> pageInfo = new PageInfo<>(list1);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public PageInfo<Integer> getRoleThrone(int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        List<Integer> list = mapper.getRoleThrone(parseInt);
        PageInfo<Integer> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public boolean deleteRoleThrone(String roleId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        int i = mapper.deleteRoleThrone(Integer.parseInt(roleId));
        SqlSessionUtil.commit();
        return i >0 ;
    }

    @Override
    public Throne getThroneById(int throneId) {

        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        Throne throne = mapper.selectByPrimaryKey(throneId);
        SqlSessionUtil.commit();
        return throne;
    }

    @Override
    public List<Throne> selectByIdStr(String throneIdStr) {

        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        List<Throne> thrones = mapper.selectByIdStr(throneIdStr);
        SqlSessionUtil.commit();
        return thrones;
    }
}
